package com.tran.app.screen

import android.annotation.SuppressLint
import android.text.TextUtils
import android.widget.Toast
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.tran.app.dao.AppDatabase
import com.tran.app.data.UserInfo
import com.tran.app.utils.show
import com.tran.app.viewmodel.RegisterViewModel
import com.tran.app.viewmodel.UpdateCourseViewModel
import com.tran.app.viewmodel.UpdateUserInfoVM
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UpdateUserInfo (modifier: Modifier,navController: NavController){
    Scaffold(modifier = Modifier.fillMaxSize(),topBar = {
        TopAppBar(title = { Text(text = "修改信息")},
            navigationIcon = {
                IconButton(onClick = { navController.popBackStack() }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "back")
                }
            })
    }) { it ->
        val scrollState = rememberScrollState()
        Column(modifier = modifier
            .padding(it)
            .verticalScroll(scrollState)
            .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally){
            val vm:UpdateUserInfoVM = viewModel()
            val uidata by vm.uidata.collectAsState()
            if(uidata.isloading){
                    Loading(modifier = modifier.size(1000.dp))
            }else{
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center) {
                    Text(text ="用户名:"+ uidata.userInfo.name)
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "邮箱")
                    },value = uidata.userInfo.email, onValueChange = {
                        vm.setuidata(uidata.copy(userInfo = uidata.userInfo.copy(email = it)))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "姓名:"+uidata.userInfo.realName)
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "性别")
                    },value = uidata.userInfo.sex, onValueChange = {
                        vm.setuidata(uidata.copy(userInfo = uidata.userInfo.copy(sex = it)))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "学号")
                    },value = uidata.userInfo.studentId, onValueChange = {
                        vm.setuidata(uidata.copy(userInfo = uidata.userInfo.copy(studentId = it)))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "系")
                    },value = uidata.userInfo.kind, onValueChange = {
                        vm.setuidata(uidata.copy(userInfo = uidata.userInfo.copy(kind = it)))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "班级")
                    },value = uidata.userInfo.grade, onValueChange = {
                        vm.setuidata(uidata.copy(userInfo = uidata.userInfo.copy(grade = it)))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "密码")
                    },value = uidata.pw, onValueChange = {
                        vm.setuidata(uidata.copy(pw = it))
                    })
                }
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp),Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(label = {
                        Text(text = "确认密码")
                    },value = uidata.pw2, onValueChange = {
                        vm.setuidata(uidata.copy(pw2 = it))
                    })
                }
                Button(modifier = Modifier
                    .padding(top = 20.dp)
                    .width(200.dp),onClick = {
                    vm.registerUser()
                }) {
                    Text(text = "提交")
                }
            }

        }
    }
}